﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FastFood.Business.Entities;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;

namespace FastFood.DataAccess
{
    public class MenuDalc
    {
        Database _menuDB;
        private MenuDalc() 
        {
            _menuDB = DatabaseFactory.CreateDatabase();
        }
        private static readonly MenuDalc _Instancia = new MenuDalc();
        public static MenuDalc Instancia
        {
            get { return MenuDalc._Instancia; }
        }

        public List<Menus> ObtenerMenusPorContenedor(byte? id)
        {
            List<Menus> lista = new List<Menus>();
            DbCommand dbCommand = _menuDB.GetStoredProcCommand("proc_MenusLoadBycontenedorID");
            _menuDB.AddInParameter(dbCommand, "contenedorID", DbType.Byte, id);
            using (IDataReader lector = _menuDB.ExecuteReader(dbCommand))
            {
                Menus menu;
                while (lector.Read())
                {
                    menu = new Menus();
                    menu.menuID = (byte)lector["menuID"];
                    menu.contenedorID = lector["contenedorID"] is DBNull? (byte?)null:(byte)lector["contenedorID"];
                    menu.nombre = (string)lector["nombre"];
                    menu.carpeta = (string)lector["carpeta"];
                    menu.descripcion = (string)lector["descripcion"];
                    menu.estado = (bool)lector["estado"];
                    
                    lista.Add(menu);
                }
            }
            return lista;
        }

        public Menus ObtenerMenuPorID(byte id)
        {
            Menus menu = new Menus();
            DbCommand dbCommand = _menuDB.GetStoredProcCommand("proc_MenusLoadByPrimaryKey");
            _menuDB.AddInParameter(dbCommand, "menuID", DbType.Byte, id);
            using (IDataReader lector = _menuDB.ExecuteReader(dbCommand))
            {
                while (lector.Read())
                {
                    menu = new Menus();
                    menu.menuID = (byte)lector["menuID"];
                    menu.contenedorID = lector["contenedorID"] is DBNull ? (byte?)null : (byte)lector["contenedorID"];
                    menu.nombre = (string)lector["nombre"];
                    menu.carpeta = (string)lector["carpeta"];
                    menu.descripcion = (string)lector["descripcion"];
                    menu.estado = (bool)lector["estado"];
                }
            }
            return menu;
        }
    }
}
